package com.xie.dome.searchservice.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.xie.dome.searchservice.config.DynamicDataSource;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据库数据源名称
 *
 * @author xie
 */
public class DataBaseUtil {

    /**
     * 数据库basic
     **/
    public static final String DB_BASIC = "basics";

    private static HashMap<String, Object> poolValueMap = new HashMap<>();

    /**
     * 加载数据库
     *
     * @param dataBaseConfigMap 数据库配置
     * @return
     */
    public static String loadDataSource(Map<String, Object> dataBaseConfigMap) {
        String key = (String) dataBaseConfigMap.get("show_name");
        String driver = (String) dataBaseConfigMap.get("driver");
        String url = (String) dataBaseConfigMap.get("url");
        String username = (String) dataBaseConfigMap.get("username");
        String password = (String) dataBaseConfigMap.get("password");

        DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
        Map<Object, Object> dataSourceMap = dynamicDataSource.getDataSourceMap();
        DruidDataSource dataSourceBasic = (DruidDataSource) dataSourceMap.get(DataBaseUtil.DB_BASIC);
        if (!dataSourceMap.containsKey(key)) {
            DruidDataSource dataSource = DataBaseUtil.initDataSource(driver, url, username, password, poolValueMap);

            Map<Object, Object> map = new HashMap<>();
            map.put(DataBaseUtil.DB_BASIC, dataSourceBasic);
            map.put(key, dataSource);

            dynamicDataSource.setTargetDataSources(map);
            // 默认数据源
            dynamicDataSource.setDefaultTargetDataSource(dataSourceBasic);
        }
        return key;
    }

    /**
     * 初始数据源
     *
     * @param driver   驱动
     * @param url      数据库连接
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public static DruidDataSource initDataSource(String driver, String url, String username, String password, HashMap<String, Object> poolMap) {

        poolValueMap.putAll(poolMap);

        //jdbc配置
        DruidDataSource rdataSource = new DruidDataSource();
        rdataSource.setDriverClassName(driver);
        rdataSource.setUrl(url);
        rdataSource.setUsername(username);
        rdataSource.setPassword(password);
        setPool(rdataSource, poolMap);
        return rdataSource;
    }

    /**
     * 连接池配置
     *
     * @param rdataSource
     */
    private static void setPool(DruidDataSource rdataSource, HashMap<String, Object> poolMap) {
        //连接池配置
        rdataSource.setMaxActive((Integer) poolMap.get("maxActive"));
        rdataSource.setMinIdle((Integer) poolMap.get("minIdle"));
        rdataSource.setInitialSize((Integer) poolMap.get("initialSize"));
        rdataSource.setMaxWait((Long) poolMap.get("maxWait"));
        rdataSource.setTimeBetweenEvictionRunsMillis((Long) poolMap.get("timeBetweenEvictionRunsMillis"));
        rdataSource.setMinEvictableIdleTimeMillis((Long) poolMap.get("minEvictableIdleTimeMillis"));
        rdataSource.setTestWhileIdle((Boolean) poolMap.get("testWhileIdle"));
        rdataSource.setTestOnBorrow((Boolean) poolMap.get("testOnBorrow"));
        rdataSource.setTestOnReturn((Boolean) poolMap.get("testOnReturn"));
        rdataSource.setValidationQuery("SELECT 'x'");
        rdataSource.setPoolPreparedStatements(true);
        rdataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        try {
            rdataSource.setFilters("stat");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
